<template>
  <!--外包 添加机构-->
  <section class="component add-organize">
    <el-form :rules="addOrganizeRules" :model="addOrgModel" ref="addOrg" label-width="100px">
      <el-form-item label="机构名称" prop="deptName">
        <el-input v-model.trim="addOrgModel.deptName" size="small"></el-input>
      </el-form-item>
      <el-form-item label="机构类型" prop="deptType">
        <el-select v-model="addOrgModel.deptType" placeholder="请选择" size="small">
          <el-option v-for="{value,label} in deptType" :key="value" :label="label" :value="value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="description">
        <el-input type="textarea" v-model="addOrgModel.description" :autosize="{ minRows:2, maxRows:4}" :maxlength="500" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label-width="0px">
        <el-row type="flex" justify="center">
          <el-button @click="close()">取消</el-button>
          <el-button @click="addOrganizeCommit">确定</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </section>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Dependencies } from '~/core/decorator';
import { State, Getter } from 'vuex-class';
import { DepartmentService } from '~/services/management-service/department.service';
import { CompanyService } from '~/services/management-service/company.service';
import CommissionSet from '~/components/case-import/client-manage/commission-set.vue'

@Component({
  components: {
    CommissionSet
  },
})
export default class AddOutsourcing extends Vue {
  @Dependencies(DepartmentService)
  private departmentService: DepartmentService;
  @Dependencies(CompanyService)
  private companyService: CompanyService;
  @State
  private companyList: any;
  @Getter
  private isSupperAdmin: boolean;
  @State
  private principalList: any;
  @State
  private userData
  private addOrgModel: any = {
    deptName: '',
    deptType: '',
    description: '',
    parentId: '1',
  };
  private deptType = [{ value: 0, label: '外包催收' }, { value: 1, label: '外包诉讼' }]
  private commissionData: any = []
  /**
   * 添加机构信息校验规则
   */
  private addOrganizeRules: any = {
    deptName: [{ required: true, message: '请输入机构名称', trigger: 'blur' }, { max: 20, message: '长度需在 20 字符之内', trigger: 'blur' }
    ],
    deptType: { required: true, message: '请选择机构类型', trigger: 'blur' },
    description: { max: 200, message: '长度需在 200 字符之内', trigger: 'blur' },
  };

  /**
   * 关闭窗口
   */
  public close() {
    const form: any = this.$refs['addOrg'];
    form.resetFields();
    this.$emit('close');
  }

  public refresh(adddata) {
    if (!adddata) {
      return;
    }
    this.addOrgModel.parentId = adddata.id;
  }

  private mounted() { }
  /**
   * 添加机构确认
   */
  private addOrganizeCommit() {
    const form: any = this.$refs['addOrg'];
    form.validate((valid) => {
      if (valid) {
        this.departmentService.createDepartment(this.addOrgModel).subscribe(
          (data) => {
            this.$message.success('新增机构成功');
            this.$emit('refresh');
            this.close();
          }
        );
      }
    });
  }
}
</script>
<style lang="less" scoped>
.data-grid {
  margin-bottom: 10px;
}

.header {
  border: solid 1px #dee1e5;
  border-bottom: 0px;
  height: 40px;
  line-height: 40px;
}
.el-select-dropdown__item.selected {
  font-weight: normal;
}
</style>
